newfile:			;Write file using bFS	IN - si, file name, ax, file length
	mov cx,ax
	mov di,si
.space
	call maloc
.size
	mov [.start],bx
	mov [.end],ax
	mov si,bx
	add si,1
	mov [si],cx
.name
	xchg si,di
	add di,1
	call copystring
.pad
	mov ax,si
	call length
	add di,ax
	mov bx,[.end]
	sub bx,2
.loop
	cmp di,bx
	jge .done
	add di,1
	mov byte [di],0
	jmp .loop
.done
ret
	.start db 0,0
	.end db 0,0

killfile:
	call findfile
	mov dx,bx
	mov si,ax
	call memclear
ret

filelist:
	mov si,void + 20
	mov dx,si
	add dx,512
.loop
	cmp byte [si],'*'
	je .found
	add si,1
	cmp si,dx
	je .done
	jmp .loop
.found
	add si,2
	cmp byte [si],'*'
	je .loop
	cmp byte [si],'0'
	je .done
	call print
	call printret
	jmp .loop
.done
ret

findfile:					;Find file and give location
	mov si,void + 20
	mov bx,0
.loop
	cmp byte [si],'*'
	je .file
	cmp byte [si],'0'
	je .err
	add si,1
	jmp .loop
.file
	mov dx,si
	add si,2
	call compare
	jc .found
	jmp .loop
.found
	sub si,2
	mov si,dx
	mov ax,si
	add si,1
	mov bx,0
	mov bl,[si]
	add bx,ax
	jmp .done
.err
	mov si,error
	call print
.done	
ret

fileman:
.loop
	mov si,.prmpt
	call print
	mov di,buffer
	call input
	
	mov di,buffer
	mov si,quit
	call compare
	jc .done

	mov si,.new
	call compare
	jc .newcmd

	mov si,.list
	call compare
	jc .listcmd

	mov si,.find
	call compare
	jc .findcmd

	mov si,.edit
	call compare
	jc .editcmd

	mov si,.type
	call compare
	jc .typecmd

	mov si,.kill
	call compare
	jc .killcmd

	jmp .loop

.newcmd
	call usernew
	jmp .loop
.listcmd
	call filelist
	jmp .loop
.findcmd
	call userfind
	jmp .loop
.editcmd
	call useredit
	jmp .loop
.typecmd
	call usertype
	jmp .loop
.killcmd
	call userkill
	jmp .loop
.done	
ret
	.prmpt db 'bFS>',0
	.new db 'new',0
	.list db 'list',0
	.find db 'find',0
	.edit db 'edit',0
	.type db 'type',0
	.kill db 'kill',0

usernew:
	mov si,.nameprmpt
	call print
	mov di,.tmp1
	call input

	mov si,.sizeprmpt
	call print
	mov di,buffer
	call input
	mov si,buffer
	call toint
	
	mov si,.tmp1
	call getsystime
	call newfile
	call getsystime
ret
	.nameprmpt db 'NAME>',0
	.sizeprmpt db 'SIZE>',0
	.tmp1 times 16 db 0

userfind:
	mov si,.nameprmpt
	call print
	mov di,buffer
	call input

	mov di,buffer
	call getsystime
	call findfile
	call getsystime

	mov dx,bx
	call tostring
	mov si,ax
	call print
	mov si,.space
	call print

	mov ax,dx
	call tostring
	mov si,ax
	call print
	call printret
ret
	.nameprmpt db 'NAME>',0	
	.space db ' - ',0

useredit:
	mov si,.name
	call print
	mov di,buffer
	call input

	mov di,buffer
	call findfile

	mov cx,ax
	push ax
	push bx

	mov si,cx
	add si,2
	call print
	call printret
	
	mov si,.prmpt
	call print
	mov di,buffer
	call input

	pop ax
	pop bx
	add bx,2
	mov ax,bx
	call length
	add bx,ax
	add bx,1
	mov si,buffer
	mov di,bx
	call copystring	
.done

ret
	.name db 'NAME>',0
	.prmpt db '>',0

usertype:
	mov si,.name
	call print
	mov di,buffer
	call input
	
	mov di,buffer
	call findfile
	
	add ax,2
	mov si,ax
	call print
	call printret
	
	call print
	call printret
ret
	.name db 'NAME>',0

userkill:
	mov si,.name
	call print
	mov di,buffer
	call input

	mov di,buffer
	call killfile
ret
	.name db 'NAME>',0

getsystime:
	pusha
	pushfd
	cli
	mov al,00000000b
	out 43h,al
	in al,40h
	mov ah,al
	in al,40h
	rol ax,8
	sti
	popfd
	call tostring
	mov si,ax
	call print
	call printret
	popa
ret